package joindemo.domain.vo;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serial;
import java.io.Serializable;
import java.util.Collections;
import java.util.List;

/**
 * 统一分页响应对象
 * @param <T> 数据类型
 */
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
@Schema(description = "标准分页响应")
public class PageVo<T> implements Serializable {

    @Serial
    private static final long serialVersionUID = 1L;

    @Schema(description = "数据列表")
    private List<T> records = Collections.emptyList();

    @Schema(description = "总记录数", example = "100")
    private Long total = 0L;

    @Schema(description = "当前页码", example = "1")
    private Integer current = 1;

    @Schema(description = "每页大小", example = "10")
    private Integer size = 10;

    @Schema(description = "总页数", example = "10")
    private Integer pages = 0;

    @Schema(description = "是否有下一页")
    private Boolean hasNext = false;

    @Schema(description = "是否有上一页")
    private Boolean hasPrevious = false;

    public static <T> PageVo<T> of(List<T> records, Long total, Integer current, Integer size) {
        int pages = size > 0 ? (int) Math.ceil((double) total / size) : 0;
        return PageVo.<T>builder()
                .records(records)
                .total(total)
                .current(current)
                .size(size)
                .pages(pages)
                .hasNext(current < pages)
                .hasPrevious(current > 1)
                .build();
    }
}